package dom.xpath.query.xpath.func;

import dom.xpath.core.proc.Check;
import dom.xpath.query.EvalException;
import dom.xpath.query.xpath.XPContext;
import dom.xpath.query.xpath.expr.Expression;
import dom.xpath.query.xpath.values.NodeSet;
import dom.xpath.query.xpath.values.Item;
import dom.xpath.util.Token;

/**
 * Constructor for the doc() and document() function.
 * 
 * @author Workgroup DBIS, University of Konstanz 2005-07, ISC License
 * @author Christian Gruen
 */
public final class Doc extends Func {
  /**
   * Function Constructor.
   * @param arg expression array
   */
  public Doc(final Expression[] arg) {
    super(arg, "doc(item)");
  }

  @Override
  public NodeSet eval(final XPContext ctx)
      throws EvalException {

    final Item arg = evalArgs(ctx)[0];
    ctx.local = new NodeSet(Check.check(Token.string(arg.str())));
    return ctx.local;
  }

  @Override
  public boolean checkArguments() {
    return args.length == 1;
  }
}
